package org.jeecg.modules.demo.huibaozhibiao.service.impl;

import lombok.extern.slf4j.Slf4j;
import org.jeecg.modules.demo.huibaozhibiao.entity.Huibaozhibiao;
import org.jeecg.modules.demo.huibaozhibiao.mapper.HuibaozhibiaoMapper;
import org.jeecg.modules.demo.huibaozhibiao.service.IHuibaozhibiaoService;
import org.jeecg.modules.demo.zhibiao.entity.Zhibiaobiao;
import org.jeecg.modules.demo.zhibiao.mapper.ZhibiaobiaoMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Locale;

/**
 * @Description: 汇报指标待办表
 * @Author: jeecg-boot
 * @Date:   2025-09-03
 * @Version: V1.0
 */
@Service
@Slf4j
public class HuibaozhibiaoServiceImpl extends ServiceImpl<HuibaozhibiaoMapper, Huibaozhibiao> implements IHuibaozhibiaoService {

    @Autowired
    private ZhibiaobiaoMapper zhibiaobiaoMapper;

    @Override
    public void generateTimedData() {
        log.info("开始执行generateTimedData方法");

        List<Zhibiaobiao> zhibiaoList = zhibiaobiaoMapper.selectList(null);
        if (zhibiaoList.isEmpty()) {
            log.info("未查询到任何指标数据，无需处理");
            return;
        }
        // 获取当前月份（中文）
        LocalDate now = LocalDate.now();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MMMM", Locale.CHINESE);
        String chineseMonth = now.format(formatter);
        log.info("当前处理月份：{}", chineseMonth);
        // 遍历指标数据，处理未完成的指标
        for (Zhibiaobiao zhibiaobiao : zhibiaoList) {
            String jindu = zhibiaobiao.getJindu();
            // 进度未达100%时，生成待办记录
            if (jindu == null || !"100%".equals(jindu.trim())) {
                log.info("处理指标：{}，当前进度：{}", zhibiaobiao.getId(), jindu);
                Huibaozhibiao huibaozhibiao = new Huibaozhibiao();
                huibaozhibiao.setZhibiaoid(zhibiaobiao.getId());
                huibaozhibiao.setZerenbumen(zhibiaobiao.getZerenbumen());
                huibaozhibiao.setHuibaoyuefen(chineseMonth);
                huibaozhibiao.setZhuabgtai("待汇报");
                this.save(huibaozhibiao);
                log.info("生成待办记录：{}", huibaozhibiao.getId());
            }
        }

        log.info("generateTimedData方法执行完成");
    }
}